﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using ReportModel;

namespace ReportView
{
   partial class MainForm
    {
        public static Setings setings;
        public void generate()
        {
            Report report;
            if (prevalidate)
            {
                report = new Report();
                report.thisWeekCellRow = int.Parse(thisWeekCellRow.Text);
                report.thisWeekCellColumn = int.Parse(thisWeekCellColumn.Text);
                report.nextWeekCellRow = int.Parse(nextWeekCellRow.Text);
                report.nextWeekCellColumn = int.Parse(nextWeekCellColumn.Text);
                if (!SelectNextWeekDateChkBx.Checked)
                {
                    DateTime thisWeekStart = Generator.GetDateByIndex(int.Parse(comboBox1.SelectedValue.ToString()));
                    DateTime thisWeekEnd = Generator.GetDateByIndex(int.Parse(comboBox2.SelectedValue.ToString()));
                    report.thisWeek = thisWeekStart.Date.ToString("M.dd") + "-" + thisWeekEnd.Date.ToString("M.dd");
                    report.nextWeek = thisWeekStart.AddDays(7).Date.ToString("M.dd") + "-" + thisWeekEnd.AddDays(7).Date.ToString("M.dd");
                    string newPath = Generator.GeneratorReport(setings.template, report);
                    if (string.Empty != newPath)
                    {
                        MessageBox.Show(new LinkLabel(), "文件已生成,路径" + newPath);
                        //MessageBox.Show(new DelayCloseForm(800), "文件已生成,路径" + newPath);

                        string v_OpenFolderPath = Directory.GetParent(newPath).FullName;
                        System.Diagnostics.Process.Start("explorer.exe", v_OpenFolderPath);
                        System.Diagnostics.Process.Start(newPath);
                    } else
                    {
                        MessageBox.Show("不好意思，生成失败了");
                    }
                     
                } else
                {
                    DateTime thisWeekStart = Generator.GetDateByIndex(int.Parse(comboBox1.SelectedValue.ToString()));
                    DateTime thisWeekEnd = Generator.GetDateByIndex(int.Parse(comboBox2.SelectedValue.ToString()));
                    report.thisWeek = thisWeekStart.Date.ToString("M.dd") + "-" + thisWeekEnd.Date.ToString("M.dd");
                    report.nextWeek = nextWeekStartDatePk.Value.ToString("M.dd") + "-" + nextWeekEndDatePk.Value.ToString("M.dd");
                    Generator.GeneratorReport(setings.template, report);
                }
            }
            
        }
        public bool prevalidate
        {
            get
            {

                if (changeTplChkbx.Checked)
                {

                    if (reportFilePath.Text.Trim() == "点击选择模板文件")
                    {
                        MessageBox.Show("请选择日志模板");
                        return false;
                    }
                    else
                    {
                        setings.template = reportFilePath.Text;
                        SerializeHelper.serializeToBinary("setings.dll", setings);
                        return true;
                    }

                }
                else
                {
                    setings = (Setings)SerializeHelper.deserializeFromBinary("setings.dll");
                    //MessageBox.Show(setings.template);
                    if (!File.Exists(setings.template))
                    {
                        MessageBox.Show("请选择日志模板");
                        return false;
                    }
                    //if (setings.template == String.Empty||setings.template == "点击选择模板文件" ||  reportFilePath.Text== "点击选择模板文件")
                    //{
                    //    MessageBox.Show("请选择日志模板");
                    //    return false;
                    //} 
                    else
                    {

                        setings.template = reportFilePath.Text;
                        return true;
                    }
                }
            }
        }

        public void readSetings()
        {

        }

        public void GenerateReport()
        {
           
             
        
        }
        public void GenerateReport(Report report)
        {
             
             
            Generator.GeneratorReport(setings.template, report);
             
        
        }

        public void initSetings()
        {
            if (!File.Exists("setings.dll"))
            {
                changeTplChkbx.Checked = true;
                setings = new Setings();
                setings.weekStart = 1;
                setings.weekEnd = 5;
                setings.thisWeekCellColumn = 1;
                setings.thisWeekCellRow = 1;
                setings.nextWeekCellRow = 6;
                setings.nextWeekCellColumn = 1;
                thisWeekCellColumn.Text= setings.thisWeekCellColumn.ToString();
                thisWeekCellRow.Text = setings.thisWeekCellRow.ToString();
                nextWeekCellRow.Text = setings.nextWeekCellRow.ToString();
                nextWeekCellColumn.Text = setings.nextWeekCellColumn.ToString();
            }
            else {
                setings = (Setings)SerializeHelper.deserializeFromBinary("setings.dll");
                thisWeekCellColumn.Text = setings.thisWeekCellColumn.ToString();
                thisWeekCellRow.Text = setings.thisWeekCellRow.ToString();
                nextWeekCellRow.Text = setings.nextWeekCellRow.ToString();
                nextWeekCellColumn.Text = setings.nextWeekCellColumn.ToString();
                if (!File.Exists(setings.template))
                {
                    changeTplChkbx.Checked = true;
                }
            }
            DaysOfWeek.bindingCombox(comboBox1, comboBox2, setings);
            if (setings.template != null)
            {
                reportFilePath.Text = setings.template?.ToString();
            }
        }
        public void saveSetings()
        {
            setings.weekStart = int.Parse(comboBox1.SelectedValue.ToString());
            setings.weekEnd = int.Parse(comboBox2.SelectedValue.ToString());
            setings.thisWeekCellColumn = int.Parse(thisWeekCellColumn.Text);
            setings.thisWeekCellRow = int.Parse(thisWeekCellRow.Text);
            setings.nextWeekCellRow = int.Parse(nextWeekCellRow.Text);
            setings.nextWeekCellColumn = int.Parse(nextWeekCellColumn.Text);
            SerializeHelper.serializeToBinary("setings.dll", setings);
        }
    }
}
